package com.yzjs.localhost.service.tNeusoftStatistics.impl;

import cn.hutool.core.date.CalendarUtil;
import cn.hutool.core.io.unit.DataUnit;
import cn.hutool.core.util.NumberUtil;
import com.yzjs.common.base.service.BaseServiceImpl;
import com.yzjs.common.util.conversion.DateUtils;
import com.yzjs.localhost.dictionaries.DealDictionary;
import com.yzjs.localhost.dto.TNeusoftStatisticsReportDTO;
import com.yzjs.localhost.entity.tNeusoftComplianceTesting.TNeusoftComplianceTestingEntity;
import com.yzjs.localhost.entity.tNeusoftStatistics.TNeusoftStatisticsEntity;
import com.yzjs.localhost.dao.tNeusoftStatistics.TNeusoftStatisticsMapper;
import com.yzjs.localhost.entity.tNeusoftUniformityTesting.TNeusoftUniformityTestingEntity;
import com.yzjs.localhost.service.tNeusoftBigDataTesting.TNeusoftBigDataTestingService;
import com.yzjs.localhost.service.tNeusoftComplianceTesting.TNeusoftComplianceTestingService;
import com.yzjs.localhost.service.tNeusoftStatistics.TNeusoftStatisticsService;
import com.yzjs.localhost.service.tNeusoftUniformityTesting.TNeusoftUniformityTestingService;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.List;


@Service("tNeusoftStatisticsService")
public class TNeusoftStatisticsServiceImpl extends BaseServiceImpl<TNeusoftStatisticsEntity, TNeusoftStatisticsMapper > implements TNeusoftStatisticsService {


    @Autowired
    private TNeusoftStatisticsMapper neusoftStatisticsMapper ;

    @Autowired
    private TNeusoftUniformityTestingService   neusoftUniformityTestingService;

    @Autowired
    private TNeusoftComplianceTestingService  neusoftComplianceTestingService ;

    @Autowired
    private TNeusoftBigDataTestingService neusoftBigDataTestingService;

    @Override
    public List<TNeusoftStatisticsEntity> getDataStatisticsSeven(TNeusoftStatisticsEntity tNeusoftStatistics) {
        Calendar calendar = DateUtils.getCalendar();
        tNeusoftStatistics.setQueryEndTime(DateUtils.formatDate(calendar,"yyyy-MM-dd"));
        calendar.add(Calendar.DATE,-6);
        tNeusoftStatistics.setQueryStartTime(DateUtils.formatDate(calendar,"yyyy-MM-dd"));
        List<TNeusoftStatisticsEntity> tNeusoftStatisticsList = neusoftStatisticsMapper.getDataStatisticsSeven(tNeusoftStatistics);
        return tNeusoftStatisticsList;
    }

    @Override
    public TNeusoftStatisticsEntity getDataStatisticCumulative() {
        TNeusoftStatisticsEntity tNeusoftStatisticsList = neusoftStatisticsMapper.getDataStatisticCumulative();

        // 获取所有累计合规性报警未处理的参数
        TNeusoftUniformityTestingEntity  neusoftUniformityTesting = new TNeusoftUniformityTestingEntity();
        neusoftUniformityTesting.setPageNum(1);
        neusoftUniformityTesting.setPageSize(1);
        neusoftUniformityTesting.setDisposalStatus(DealDictionary.UNTREATED);
        neusoftUniformityTestingService.findList(neusoftUniformityTesting);
        tNeusoftStatisticsList.setUniformityDataCount(neusoftUniformityTesting.getTotalRowCount().intValue());

        // 获取所有累计一致性报警未处理的
        TNeusoftComplianceTestingEntity neusoftComplianceTesting = new TNeusoftComplianceTestingEntity();
        neusoftComplianceTesting.setPageNum(1);
        neusoftComplianceTesting.setPageSize(1);
        neusoftComplianceTesting.setDisposalStatus(DealDictionary.UNTREATED);
        neusoftComplianceTestingService.findList(neusoftComplianceTesting);
        tNeusoftStatisticsList.setComplianceDataCount(neusoftComplianceTesting.getTotalRowCount().intValue());
        return tNeusoftStatisticsList;
    }

    /**
     * 获取统计报告
     * @return
     */
    @Override
    public TNeusoftStatisticsReportDTO getDataStatisticReport() {
        TNeusoftStatisticsEntity tNeusoftStatistics = new TNeusoftStatisticsEntity();
        Calendar calendar = DateUtils.getCalendar();
        tNeusoftStatistics.setQueryEndTime(DateUtils.formatDate(calendar,"yyyy-MM-dd"));
        calendar.add(Calendar.DATE,-6);
        tNeusoftStatistics.setQueryStartTime(DateUtils.formatDate(calendar,"yyyy-MM-dd"));
        List<TNeusoftStatisticsEntity> thisWeekList = neusoftStatisticsMapper.getDataStatisticsSeven(tNeusoftStatistics);

        calendar.add(Calendar.DATE,-1);
        tNeusoftStatistics.setQueryEndTime(DateUtils.formatDate(calendar,"yyyy-MM-dd"));
        calendar.add(Calendar.DATE,-6);
        tNeusoftStatistics.setQueryStartTime(DateUtils.formatDate(calendar,"yyyy-MM-dd"));
        List<TNeusoftStatisticsEntity> lastWeekList = neusoftStatisticsMapper.getDataStatisticsSeven(tNeusoftStatistics);



        calendar = DateUtils.getCalendar();
        String beginDate = DateUtils.formatDate(calendar,"yyyy-MM-dd");
        calendar.add(Calendar.DATE,-6);
        String enDate = DateUtils.formatDate(calendar,"yyyy-MM-dd");

        //获取本周预警量
        Integer uniformityThisWeek = thisWeekList.stream().mapToInt(TNeusoftStatisticsEntity::getUniformityDataCount).sum();
        Integer complianceThisWeek = thisWeekList.stream().mapToInt(TNeusoftStatisticsEntity::getComplianceDataCount).sum();
        Integer dataVerificationThisWeek = thisWeekList.stream().mapToInt(TNeusoftStatisticsEntity::getBigDataVerification).sum();


        Integer saveNum =  thisWeekList.stream().mapToInt(TNeusoftStatisticsEntity::getBehaviorDataCount).sum();
        saveNum = saveNum+  thisWeekList.stream().mapToInt(TNeusoftStatisticsEntity::getBusinessDataCount).sum();
        // 上周预警量
        Integer uniformityLastWeek = lastWeekList.stream().mapToInt(TNeusoftStatisticsEntity::getUniformityDataCount).sum();
        Integer complianceLastWeek = lastWeekList.stream().mapToInt(TNeusoftStatisticsEntity::getComplianceDataCount).sum();
        Integer dataVerificationLastWeek = lastWeekList.stream().mapToInt(TNeusoftStatisticsEntity::getBigDataVerification).sum();


        String uniformityGrowthRate ="/";
        if(!uniformityLastWeek.equals(0)){
             uniformityGrowthRate = NumberUtil.roundStr((uniformityThisWeek-uniformityLastWeek)*100/uniformityLastWeek,4);
        }

        String complianceGrowthRate ="/";
        if (!complianceLastWeek.equals(0)){
             complianceGrowthRate = NumberUtil.roundStr((complianceThisWeek-complianceLastWeek)*100/complianceLastWeek,4);
        }
        String dataVerificationGrowthRate ="/";
        if (!dataVerificationLastWeek.equals(0)){
            dataVerificationGrowthRate = NumberUtil.roundStr((dataVerificationThisWeek-dataVerificationLastWeek)*100/dataVerificationLastWeek,4);
        }

        String systemState ="稳定";
        if ((uniformityThisWeek+complianceThisWeek)>=1 && (uniformityThisWeek+complianceThisWeek)<50 ){
            systemState ="增长";
        }else if((uniformityThisWeek+complianceThisWeek)>=50){
            systemState ="增长";
        }

        String systemStateTrend ="稳定状态";
        if((uniformityLastWeek+ complianceLastWeek)==0 &&(uniformityThisWeek+ complianceThisWeek)==0) {
            systemStateTrend = "稳定状态";
        }  else if((uniformityLastWeek+ complianceLastWeek)==0 &&(uniformityThisWeek+ complianceThisWeek)>=0) {
            systemStateTrend = "不稳定状态";
        }
        else if((uniformityThisWeek+uniformityThisWeek-uniformityLastWeek+complianceLastWeek)*100/(uniformityLastWeek+ complianceLastWeek)>0
        && (uniformityThisWeek+uniformityThisWeek-uniformityLastWeek+complianceLastWeek)*100/(uniformityLastWeek+ complianceLastWeek)<=40){
            systemStateTrend = "不稳定状态";
        }else if ((uniformityThisWeek+uniformityThisWeek-uniformityLastWeek+complianceLastWeek)*100/(uniformityLastWeek+ complianceLastWeek)>40){
            systemStateTrend = "接近危险";
        }

        String proposal ="一切良好";
        if((uniformityThisWeek+uniformityThisWeek)>1&&(uniformityThisWeek+uniformityThisWeek)<=50 ){
            proposal="请关注系统健康情况";
        }else if ( (uniformityThisWeek+uniformityThisWeek)>50){
            proposal="请联系管理员";
        }
        TNeusoftStatisticsReportDTO dto = new TNeusoftStatisticsReportDTO(beginDate,enDate,
                uniformityThisWeek,uniformityLastWeek,uniformityGrowthRate,complianceThisWeek,complianceLastWeek,
                complianceGrowthRate,systemState ,systemStateTrend,proposal,saveNum,thisWeekList,dataVerificationThisWeek,
                dataVerificationLastWeek, dataVerificationGrowthRate);

        return dto;
    }
}